/*
 * @Author: 脉迈
 * @Date: 2022-01-17 16:31:21
 * @LastEditors: 脉迈
 * @LastEditTime: 2022-01-28 14:58:37
 */
module.exports = app => ({
  async create (ctx,next) {
    let {$models, $helper} = app;
    let PageTemplate = $models.PageTemplate;
    let {header,body} = ctx.request;
    let {name,data} = body;
    let {token} = header;
    let userData = await $helper.checkToken(token)
    await PageTemplate.create({
      name,
      data,
      source_user_id: userData.data.id
    })
    .then((result) => {
      ctx.body = {
        message:'保存成功',
        code: 200
      }
    })
    .catch(err => {
      ctx.body = {
        message:'保存失败',
        code: 500
      }
    })
  },
  async getList(ctx,next){
    let {$models, $helper} = app;
    let {header,body} = ctx.request;
    let {token} = header;
    let {type} = body;
    let userData = await $helper.checkToken(token)
    let PageTemplate = $models.PageTemplate;
    if(type){
      await PageTemplate.findAll({
        source_user_id:  type === 'official' ? 28 : userData.data.id,
      })
      .then((result)=>{
        ctx.body({
          code:200,
          data:result,
          message:''
        })
      })
      .catch(err=>{
        ctx.body = {
          message:'读取失败',
          code: 500
        }
      })
    }else{
      await PageTemplate.findAll()
      .then((result)=>{
        if(result.length){
          let offical = []
          let private = []
          result.map((item)=>{
            if(item.source_user_id === 28){
              offical.push(item)
            }else{
              private.push(item)
            }
          })
          ctx.body = {
            code:200,
            data: {
              offical,
              private
            },
            message:''
          }
        }else{
          ctx.body = {
            code:200,
            data: {
              offical:[],
              private:[]
            },
            message:''
          }
        }
        
      })
      .catch(err=>{
        console.log(err)
        ctx.body = {
          message:'读取失败2',
          err:err,
          code: 500
        }
      })
    }
    
  },
  async getDetail(ctx,next){
    let {$models, $helper} = app;
    let PageTemplate = $models.PageTemplate;
    let {id} = ctx.request.query;
    await PageTemplate.findOne({
      where: {
        id
      }
    })
    .then((result) => {
      ctx.body = {
        message:'读取成功',
        data:result,
        code: 200
      }
    })
    .catch(err => {
      ctx.body = {
        message:'读取失败',
        code: 500
      }
    })
  },
  async detail(ctx,next){
    let {$models, $helper} = app;
    let PageTemplate = $models.PageTemplate;
    let {id} = ctx.request.query;
    await PageTemplate.findOne({
      where: {
        id
      }
    })
    .then((result) => {
      ctx.body = {
        message:'读取成功',
        data:result,
        code: 200
      }
    })
    .catch(err => {
      ctx.body = {
        message:'读取失败',
        code: 500
      }
    })
  }
})